package com.huirui.server.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huirui.server.context.UserContext;
import com.huirui.server.mapper.FeedbackMapper;
import com.huirui.server.pojo.dto.FeedbackDto;
import com.huirui.server.pojo.entity.Feedback;
import com.huirui.server.pojo.result.PageResult;
import com.huirui.server.pojo.vo.FeedbackVo;
import com.huirui.server.service.FeedbackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @author yujun
 * @description 针对表【t_feedback(用户反馈表)】的数据库操作Service实现
 * @createDate 2025-09-20 10:25:11
 */
@Service
@Slf4j
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback>
        implements FeedbackService {

    @Override
    public void saveUserFeedback(FeedbackDto feedbackDto) {
        Feedback feedback = new Feedback();
        feedback.setContent(feedbackDto.getFeedback());
        feedback.setUserId(UserContext.getId());
        feedback.setStatus(0);
        feedback.setCreatedAt(LocalDateTime.now());
        save(feedback);
    }

    @Override
    public PageResult<FeedbackVo> listConditional(Integer type, Integer status, LocalDate startTime, LocalDate endTime, String keyword, Long page, Long pageSize) {
        IPage<FeedbackVo> iPage = new Page<>(page, pageSize);
        iPage = baseMapper
                .listFeedbackConditional(type, status,
                        startTime != null ? startTime.atStartOfDay() : null,
                        endTime != null ? endTime.atTime(LocalTime.MAX) : null,
                        keyword, iPage);
        return PageResult.from(iPage);
    }

    @Override
    public void handleFeedback(FeedbackDto feedbackDto) {
        lambdaUpdate()
                .eq(Feedback::getId, feedbackDto.getFeedbackId())
                .set(Feedback::getRemark, feedbackDto.getRemark())
                .set(Feedback::getStatus, feedbackDto.getFeedbackStatus())
                .set(Feedback::getHandledAt, LocalDateTime.now())
                .update();
        if (feedbackDto.getNotify()) {
            // TODO 通知用户相关逻辑
        }
    }
}




